﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ServiceModel.DomainServices.Client;

namespace ExpressionSerialization
{
  [TestClass]
  public class Develop
  {
    Demo.Web.TestDomainContext dc = new Demo.Web.TestDomainContext();

    [TestMethod()]
    public void Test01()
    {
      System.Linq.Expressions.Expression expRoot = null;
      Expression<Func<Demo.Web.Product, bool>> cc;
      // Price from
      cc = p => p.ListPrice >= decimal.Parse("500.1",System.Globalization.CultureInfo.InvariantCulture);
      expRoot = expRoot.LogicalAnd(cc);

      //Price to
      cc = p => p.ListPrice <= decimal.Parse("999.9", System.Globalization.CultureInfo.InvariantCulture);
      expRoot = expRoot.LogicalAnd(cc);

      //Size from
      cc = p => p.Size >= decimal.Parse("44");
      expRoot = expRoot.LogicalAnd(cc);
      //Size from
      cc = p => p.Size <= decimal.Parse("52");
      expRoot = expRoot.LogicalAnd(cc);


      string[] bikeColors = new String[] {"Red","Black"};
      foreach(var item in bikeColors) {
        if(!string.IsNullOrEmpty(item)) {
          //string ss = item;
          cc = p => p.Color.ToUpper().Contains(item.ToUpper());
          expRoot = expRoot.LogicalOr(cc);
        }
      }

      byte[] aa = expRoot.Serialize();

      EntityQuery<Demo.Web.Product> qq = dc.GetProductQuery(aa);
      dc.Load(qq, (p) => {
        int ii = (p.Entities as ReadOnlyCollection<Demo.Web.Product>).Count;
        Assert.AreEqual(ii, 112);
      }, null);
    }

  }
}
